import JsCookie from '@/utils/cookies'
const Cookies = new JsCookie('TENANT_ID')

const authModule = {
    state: {
        token: localStorage.getItem('token'),
        tenantId: Cookies.get() || localStorage.getItem('tenantId') || 1
    },
    mutations: {
        SET_TOKEN (state, data) {
            if (data) {
                const { token, tenantId } = data
                if (token) {
                    state.token = token
                    localStorage.setItem('token', token)
                }
                if (tenantId) {
                    state.token = tenantId
                    localStorage.setItem('tenantId', tenantId)
                }
            }
        }
    },
    actions: {
        setToken ({ commit }, data) {
            commit('SET_TOKEN', data)
        }
    }
}

export default authModule